package com.iflysse.services.impl;

import com.iflysse.dao.CourseDao;
import com.iflysse.dao.LabelDao;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.AddCourseBoReq;
import com.iflysse.pojo.bo.req.CourseIdBoReq;
import com.iflysse.pojo.bo.req.TeacherIdBoReq;
import com.iflysse.pojo.bo.req.UpdateCourseBoReq;
import com.iflysse.pojo.bo.resp.CourseAllBoResp;
import com.iflysse.pojo.bo.resp.CourseBoResp;
import com.iflysse.pojo.bo.resp.CourseDetailBoResp;
import com.iflysse.pojo.bo.resp.LabelBoResp;
import com.iflysse.pojo.po.req.CourseIdPoReq;
import com.iflysse.pojo.po.req.CoursePoReq;
import com.iflysse.pojo.po.req.LabelIdsPoReq;
import com.iflysse.pojo.po.resp.*;
import com.iflysse.services.CourseService;
import com.iflysse.util.CommonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class CourseServiceImpl implements CourseService {
    public static final Logger log = Logger.getLogger(ActivityServiceImpl.class);
    @Autowired
    CourseDao courseDao ;
    @Autowired
    LabelDao labelDao;

    /**
     * 添加课程
     *
     * @param addCourseBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp addCourse(AddCourseBoReq addCourseBoReq) {
        CommonBoResp commonBoResp = new CommonBoResp();

        //实体转化
        CoursePoReq coursePoReq = CommonUtil.objToObj(addCourseBoReq, CoursePoReq.class);
        try {
            coursePoReq.setCourseId(UUID.randomUUID().toString());
            int r1 = courseDao.addCourse(coursePoReq);
            int r2 = courseDao.addCourseLabel(coursePoReq);
            CommonUtil.printToBoResult(commonBoResp,1);
        }catch (SQLException e){
                    CommonUtil.printToBoResult(commonBoResp,0);
        }
        return commonBoResp;
    }

    /**
     * 修改课程
     * @param updateCourseBoReq 课程
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp updateCourse(UpdateCourseBoReq updateCourseBoReq) {
        CommonBoResp commonBoResp = new CommonBoResp();
        //实体转化
        CoursePoReq coursePoReq = CommonUtil.objToObj(updateCourseBoReq, CoursePoReq.class);

        try {

            //修改课程信息
            courseDao.modifyCourse(coursePoReq);
            //判断用户是否修改了标签
            //根据课程编号获取对应的标签编号
            CourseIdPoReq courseIdPoReq = new CourseIdPoReq();
            courseIdPoReq.setCourseId(updateCourseBoReq.getCourseId());
            LabelIdsPoResp labelIdsPoResp = new LabelIdsPoResp();
            List<String> labelIdsByCourseId = labelDao.getLabelIdsByCourseId(courseIdPoReq);
            labelIdsPoResp.setLabelIds(labelIdsByCourseId);
            //是否需要更新标签
            boolean needUpdateLabel = false;
            if(updateCourseBoReq.getLabelIdList().size() != labelIdsPoResp.getLabelIds().size() ){
                needUpdateLabel = true;
            }else{
                for(int i =0;i<updateCourseBoReq.getLabelIdList().size();i++){
                    //判断用户提交的标签和原来的标签是否一致
                    if(!updateCourseBoReq.getLabelIdList().contains(labelIdsPoResp.getLabelIds().get(i))) {
                        needUpdateLabel= true;

                    }
                }
            }
            //需要更新标签列表
            if(needUpdateLabel) {
                //删除课程原来的标签
                courseDao.deleteCourseLabelByCourseId(coursePoReq.getCourseId());
                //重新添加新标签
                courseDao.addCourseLabel(coursePoReq);
            }
            CommonUtil.printToBoResult(commonBoResp,1);
        }catch (SQLException e){
                CommonUtil.printToBoResult(commonBoResp,0);

        }
        return commonBoResp;
    }

    /**
     * 根据老师编号查询老师的6门课程
     * @param teacherIdBoReq
     * @return
     * @throws SQLException
     */
    @Override
    public CourseAllBoResp queryCourseTop6ByTeacherId(TeacherIdBoReq teacherIdBoReq) throws SQLException {
        CourseAllBoResp resp = new CourseAllBoResp();
        List<CoursePoResp> coursePoRespList = courseDao.getTop6CourseByTeacherId(teacherIdBoReq.getTeacherId());

        convertPo2Bo(resp, coursePoRespList);

        return resp;
    }

    /**
     * 将集合中的Po对象转换BO对象
     * @param resp
     * @param coursePoRespList
     */
    private void convertPo2Bo(CourseAllBoResp resp, List<CoursePoResp> coursePoRespList) {
        CourseBoResp bo = null;
        for (CoursePoResp coursePoResp : coursePoRespList) {
            bo = new CourseBoResp();
            bo.setCourseId(coursePoResp.getCourseId());
            bo.setCourseCover(coursePoResp.getCourseCover());
            bo.setUpdateTime(coursePoResp.getUpdateTime());
            bo.setCourseName(coursePoResp.getCourseName());
            resp.getCourseBoResps().add(bo);
        }
    }

    /**
     * 根据老师编号查询老师的6门课程
     * @param teacherIdBoReq
     * @return
     * @throws SQLException
     */
    @Override
    public CourseAllBoResp queryAllCourseByTeacherId(TeacherIdBoReq teacherIdBoReq) throws SQLException {
        CourseAllBoResp resp = new CourseAllBoResp();
        List<CoursePoResp> coursePoRespList = courseDao.getAllCoursesByTeacherId(teacherIdBoReq.getTeacherId());
        convertPo2Bo(resp, coursePoRespList);
        return resp;
    }

    /**
     * 根据课程编号获取课程详情
     *
     * @param courseIdBoReq
     * @return
     */
    @Override
    public CourseDetailBoResp queryDetailByCourseId(CourseIdBoReq courseIdBoReq) throws SQLException {

        //参数转换
        CourseIdPoReq courseIdPoReq = CommonUtil.objToObj(courseIdBoReq, CourseIdPoReq.class);
        //获取课程详情
        CourseDetailPoResp courseDetailPoResp = courseDao.getDetailByCourseId(courseIdPoReq);
        LabelIdsPoResp labelIdsPoResp = new LabelIdsPoResp();
        //根据课程编号获取对应的标签编号
        List<String> labelIdsByCourseId = labelDao.getLabelIdsByCourseId(courseIdPoReq);
        labelIdsPoResp.setLabelIds(labelIdsByCourseId);
        LabelIdsPoReq labelIdsPoReq = CommonUtil.objToObj(labelIdsPoResp, LabelIdsPoReq.class);
        CourseDetailBoResp courseDetailBoResp = CommonUtil.objToObj(courseDetailPoResp, CourseDetailBoResp.class);

        //根据标签编号获取标签
        LabelAllPoResp labels = null;
        //如果存在标签,就去查询
        if(labelIdsPoReq.getLabelIds().size() > 0) {
            labels=new LabelAllPoResp();

            List<LabelPoResp> labelsByLabelIds = labelDao.getLabelsByLabelIds(labelIdsPoReq);
            labels.setLabelAllPoResp(labelsByLabelIds);
            //将标签名称添加到课程中
            List<LabelBoResp>courseLabels=new ArrayList<LabelBoResp>();

            //将po转bo，存入集合
            labels.getLabelAllPoResp().forEach(labelPoResp -> {
                courseLabels.add(CommonUtil.objToObj(labelPoResp, LabelBoResp.class));
            });
            courseDetailBoResp.setCourseLabels(courseLabels);
        }

        return courseDetailBoResp;
    }

}
